<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>stringmatcher.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_testing_stringmatcher.js.html">stringmatcher.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides the built-in string matchers like containsString,
<a name="line17"></a> *     startsWith, endsWith, etc.
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.labs.testing.ContainsStringMatcher&#39;);
<a name="line22"></a>goog.provide(&#39;goog.labs.testing.EndsWithMatcher&#39;);
<a name="line23"></a>goog.provide(&#39;goog.labs.testing.EqualToIgnoringCaseMatcher&#39;);
<a name="line24"></a>goog.provide(&#39;goog.labs.testing.EqualToIgnoringWhitespaceMatcher&#39;);
<a name="line25"></a>goog.provide(&#39;goog.labs.testing.EqualsMatcher&#39;);
<a name="line26"></a>goog.provide(&#39;goog.labs.testing.RegexMatcher&#39;);
<a name="line27"></a>goog.provide(&#39;goog.labs.testing.StartsWithMatcher&#39;);
<a name="line28"></a>goog.provide(&#39;goog.labs.testing.StringContainsInOrderMatcher&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line32"></a>goog.require(&#39;goog.labs.testing.Matcher&#39;);
<a name="line33"></a>goog.require(&#39;goog.string&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * The ContainsString matcher.
<a name="line39"></a> *
<a name="line40"></a> * @param {string} value The expected string.
<a name="line41"></a> *
<a name="line42"></a> * @constructor
<a name="line43"></a> * @implements {goog.labs.testing.Matcher}
<a name="line44"></a> */
<a name="line45"></a>goog.labs.testing.ContainsStringMatcher = function(value) {
<a name="line46"></a>  /**
<a name="line47"></a>   * @type {string}
<a name="line48"></a>   * @private
<a name="line49"></a>   */
<a name="line50"></a>  this.value_ = value;
<a name="line51"></a>};
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/**
<a name="line55"></a> * Determines if input string contains the expected string.
<a name="line56"></a> *
<a name="line57"></a> * @override
<a name="line58"></a> */
<a name="line59"></a>goog.labs.testing.ContainsStringMatcher.prototype.matches =
<a name="line60"></a>    function(actualValue) {
<a name="line61"></a>  goog.asserts.assertString(actualValue);
<a name="line62"></a>  return goog.string.contains(actualValue, this.value_);
<a name="line63"></a>};
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * @override
<a name="line68"></a> */
<a name="line69"></a>goog.labs.testing.ContainsStringMatcher.prototype.describe =
<a name="line70"></a>    function(actualValue) {
<a name="line71"></a>  return actualValue + &#39; does not contain &#39; + this.value_;
<a name="line72"></a>};
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * The EndsWith matcher.
<a name="line78"></a> *
<a name="line79"></a> * @param {string} value The expected string.
<a name="line80"></a> *
<a name="line81"></a> * @constructor
<a name="line82"></a> * @implements {goog.labs.testing.Matcher}
<a name="line83"></a> */
<a name="line84"></a>goog.labs.testing.EndsWithMatcher = function(value) {
<a name="line85"></a>  /**
<a name="line86"></a>   * @type {string}
<a name="line87"></a>   * @private
<a name="line88"></a>   */
<a name="line89"></a>  this.value_ = value;
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Determines if input string ends with the expected string.
<a name="line95"></a> *
<a name="line96"></a> * @override
<a name="line97"></a> */
<a name="line98"></a>goog.labs.testing.EndsWithMatcher.prototype.matches = function(actualValue) {
<a name="line99"></a>  goog.asserts.assertString(actualValue);
<a name="line100"></a>  return goog.string.endsWith(actualValue, this.value_);
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * @override
<a name="line106"></a> */
<a name="line107"></a>goog.labs.testing.EndsWithMatcher.prototype.describe =
<a name="line108"></a>    function(actualValue) {
<a name="line109"></a>  return actualValue + &#39; does not end with &#39; + this.value_;
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * The EqualToIgnoringWhitespace matcher.
<a name="line116"></a> *
<a name="line117"></a> * @param {string} value The expected string.
<a name="line118"></a> *
<a name="line119"></a> * @constructor
<a name="line120"></a> * @implements {goog.labs.testing.Matcher}
<a name="line121"></a> */
<a name="line122"></a>goog.labs.testing.EqualToIgnoringWhitespaceMatcher = function(value) {
<a name="line123"></a>  /**
<a name="line124"></a>   * @type {string}
<a name="line125"></a>   * @private
<a name="line126"></a>   */
<a name="line127"></a>  this.value_ = value;
<a name="line128"></a>};
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>/**
<a name="line132"></a> * Determines if input string contains the expected string.
<a name="line133"></a> *
<a name="line134"></a> * @override
<a name="line135"></a> */
<a name="line136"></a>goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.matches =
<a name="line137"></a>    function(actualValue) {
<a name="line138"></a>  goog.asserts.assertString(actualValue);
<a name="line139"></a>  var string1 = goog.string.collapseWhitespace(actualValue);
<a name="line140"></a>
<a name="line141"></a>  return goog.string.caseInsensitiveCompare(this.value_, string1) === 0;
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * @override
<a name="line147"></a> */
<a name="line148"></a>goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.describe =
<a name="line149"></a>    function(actualValue) {
<a name="line150"></a>  return actualValue + &#39; is not equal(ignoring whitespace) to &#39; + this.value_;
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * The Equals matcher.
<a name="line157"></a> *
<a name="line158"></a> * @param {string} value The expected string.
<a name="line159"></a> *
<a name="line160"></a> * @constructor
<a name="line161"></a> * @implements {goog.labs.testing.Matcher}
<a name="line162"></a> */
<a name="line163"></a>goog.labs.testing.EqualsMatcher = function(value) {
<a name="line164"></a>  /**
<a name="line165"></a>   * @type {string}
<a name="line166"></a>   * @private
<a name="line167"></a>   */
<a name="line168"></a>  this.value_ = value;
<a name="line169"></a>};
<a name="line170"></a>
<a name="line171"></a>
<a name="line172"></a>/**
<a name="line173"></a> * Determines if input string is equal to the expected string.
<a name="line174"></a> *
<a name="line175"></a> * @override
<a name="line176"></a> */
<a name="line177"></a>goog.labs.testing.EqualsMatcher.prototype.matches = function(actualValue) {
<a name="line178"></a>  goog.asserts.assertString(actualValue);
<a name="line179"></a>  return this.value_ === actualValue;
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/**
<a name="line184"></a> * @override
<a name="line185"></a> */
<a name="line186"></a>goog.labs.testing.EqualsMatcher.prototype.describe =
<a name="line187"></a>    function(actualValue) {
<a name="line188"></a>  return actualValue + &#39; is not equal to &#39; + this.value_;
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * The MatchesRegex matcher.
<a name="line195"></a> *
<a name="line196"></a> * @param {!RegExp} regex The expected regex.
<a name="line197"></a> *
<a name="line198"></a> * @constructor
<a name="line199"></a> * @implements {goog.labs.testing.Matcher}
<a name="line200"></a> */
<a name="line201"></a>goog.labs.testing.RegexMatcher = function(regex) {
<a name="line202"></a>  /**
<a name="line203"></a>   * @type {!RegExp}
<a name="line204"></a>   * @private
<a name="line205"></a>   */
<a name="line206"></a>  this.regex_ = regex;
<a name="line207"></a>};
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * Determines if input string is equal to the expected string.
<a name="line212"></a> *
<a name="line213"></a> * @override
<a name="line214"></a> */
<a name="line215"></a>goog.labs.testing.RegexMatcher.prototype.matches = function(
<a name="line216"></a>    actualValue) {
<a name="line217"></a>  goog.asserts.assertString(actualValue);
<a name="line218"></a>  return this.regex_.test(actualValue);
<a name="line219"></a>};
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * @override
<a name="line224"></a> */
<a name="line225"></a>goog.labs.testing.RegexMatcher.prototype.describe =
<a name="line226"></a>    function(actualValue) {
<a name="line227"></a>  return actualValue + &#39; does not match &#39; + this.regex_;
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>/**
<a name="line233"></a> * The StartsWith matcher.
<a name="line234"></a> *
<a name="line235"></a> * @param {string} value The expected string.
<a name="line236"></a> *
<a name="line237"></a> * @constructor
<a name="line238"></a> * @implements {goog.labs.testing.Matcher}
<a name="line239"></a> */
<a name="line240"></a>goog.labs.testing.StartsWithMatcher = function(value) {
<a name="line241"></a>  /**
<a name="line242"></a>   * @type {string}
<a name="line243"></a>   * @private
<a name="line244"></a>   */
<a name="line245"></a>  this.value_ = value;
<a name="line246"></a>};
<a name="line247"></a>
<a name="line248"></a>
<a name="line249"></a>/**
<a name="line250"></a> * Determines if input string starts with the expected string.
<a name="line251"></a> *
<a name="line252"></a> * @override
<a name="line253"></a> */
<a name="line254"></a>goog.labs.testing.StartsWithMatcher.prototype.matches = function(actualValue) {
<a name="line255"></a>  goog.asserts.assertString(actualValue);
<a name="line256"></a>  return goog.string.startsWith(actualValue, this.value_);
<a name="line257"></a>};
<a name="line258"></a>
<a name="line259"></a>
<a name="line260"></a>/**
<a name="line261"></a> * @override
<a name="line262"></a> */
<a name="line263"></a>goog.labs.testing.StartsWithMatcher.prototype.describe =
<a name="line264"></a>    function(actualValue) {
<a name="line265"></a>  return actualValue + &#39; does not start with &#39; + this.value_;
<a name="line266"></a>};
<a name="line267"></a>
<a name="line268"></a>
<a name="line269"></a>
<a name="line270"></a>/**
<a name="line271"></a> * The StringContainsInOrdermatcher.
<a name="line272"></a> *
<a name="line273"></a> * @param {Array.&lt;string&gt;} values The expected string values.
<a name="line274"></a> *
<a name="line275"></a> * @constructor
<a name="line276"></a> * @implements {goog.labs.testing.Matcher}
<a name="line277"></a> */
<a name="line278"></a>goog.labs.testing.StringContainsInOrderMatcher = function(values) {
<a name="line279"></a>  /**
<a name="line280"></a>   * @type {Array.&lt;string&gt;}
<a name="line281"></a>   * @private
<a name="line282"></a>   */
<a name="line283"></a>  this.values_ = values;
<a name="line284"></a>};
<a name="line285"></a>
<a name="line286"></a>
<a name="line287"></a>/**
<a name="line288"></a> * Determines if input string contains, in order, the expected array of strings.
<a name="line289"></a> *
<a name="line290"></a> * @override
<a name="line291"></a> */
<a name="line292"></a>goog.labs.testing.StringContainsInOrderMatcher.prototype.matches =
<a name="line293"></a>    function(actualValue) {
<a name="line294"></a>  goog.asserts.assertString(actualValue);
<a name="line295"></a>  var currentIndex, previousIndex = 0;
<a name="line296"></a>  for (var i = 0; i &lt; this.values_.length; i++) {
<a name="line297"></a>    currentIndex = goog.string.contains(actualValue, this.values_[i]);
<a name="line298"></a>    if (currentIndex &lt; 0 || currentIndex &lt; previousIndex) {
<a name="line299"></a>      return false;
<a name="line300"></a>    }
<a name="line301"></a>    previousIndex = currentIndex;
<a name="line302"></a>  }
<a name="line303"></a>  return true;
<a name="line304"></a>};
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>/**
<a name="line308"></a> * @override
<a name="line309"></a> */
<a name="line310"></a>goog.labs.testing.StringContainsInOrderMatcher.prototype.describe =
<a name="line311"></a>    function(actualValue) {
<a name="line312"></a>  return actualValue + &#39; does not contain the expected values in order.&#39;;
<a name="line313"></a>};
<a name="line314"></a>
<a name="line315"></a>
<a name="line316"></a>/**
<a name="line317"></a> * Matches a string containing the given string.
<a name="line318"></a> *
<a name="line319"></a> * @param {string} value The expected value.
<a name="line320"></a> *
<a name="line321"></a> * @return {!goog.labs.testing.ContainsStringMatcher} A
<a name="line322"></a> *     ContainsStringMatcher.
<a name="line323"></a> */
<a name="line324"></a>function containsString(value) {
<a name="line325"></a>  return new goog.labs.testing.ContainsStringMatcher(value);
<a name="line326"></a>}
<a name="line327"></a>
<a name="line328"></a>
<a name="line329"></a>/**
<a name="line330"></a> * Matches a string that ends with the given string.
<a name="line331"></a> *
<a name="line332"></a> * @param {string} value The expected value.
<a name="line333"></a> *
<a name="line334"></a> * @return {!goog.labs.testing.EndsWithMatcher} A
<a name="line335"></a> *     EndsWithMatcher.
<a name="line336"></a> */
<a name="line337"></a>function endsWith(value) {
<a name="line338"></a>  return new goog.labs.testing.EndsWithMatcher(value);
<a name="line339"></a>}
<a name="line340"></a>
<a name="line341"></a>
<a name="line342"></a>/**
<a name="line343"></a> * Matches a string that equals (ignoring whitespace) the given string.
<a name="line344"></a> *
<a name="line345"></a> * @param {string} value The expected value.
<a name="line346"></a> *
<a name="line347"></a> * @return {!goog.labs.testing.EqualToIgnoringWhitespaceMatcher} A
<a name="line348"></a> *     EqualToIgnoringWhitespaceMatcher.
<a name="line349"></a> */
<a name="line350"></a>function equalToIgnoringWhitespace(value) {
<a name="line351"></a>  return new goog.labs.testing.EqualToIgnoringWhitespaceMatcher(value);
<a name="line352"></a>}
<a name="line353"></a>
<a name="line354"></a>
<a name="line355"></a>/**
<a name="line356"></a> * Matches a string that equals the given string.
<a name="line357"></a> *
<a name="line358"></a> * @param {string} value The expected value.
<a name="line359"></a> *
<a name="line360"></a> * @return {!goog.labs.testing.EqualsMatcher} A EqualsMatcher.
<a name="line361"></a> */
<a name="line362"></a>function equals(value) {
<a name="line363"></a>  return new goog.labs.testing.EqualsMatcher(value);
<a name="line364"></a>}
<a name="line365"></a>
<a name="line366"></a>
<a name="line367"></a>/**
<a name="line368"></a> * Matches a string against a regular expression.
<a name="line369"></a> *
<a name="line370"></a> * @param {!RegExp} regex The expected regex.
<a name="line371"></a> *
<a name="line372"></a> * @return {!goog.labs.testing.RegexMatcher} A RegexMatcher.
<a name="line373"></a> */
<a name="line374"></a>function matchesRegex(regex) {
<a name="line375"></a>  return new goog.labs.testing.RegexMatcher(regex);
<a name="line376"></a>}
<a name="line377"></a>
<a name="line378"></a>
<a name="line379"></a>/**
<a name="line380"></a> * Matches a string that starts with the given string.
<a name="line381"></a> *
<a name="line382"></a> * @param {string} value The expected value.
<a name="line383"></a> *
<a name="line384"></a> * @return {!goog.labs.testing.StartsWithMatcher} A
<a name="line385"></a> *     StartsWithMatcher.
<a name="line386"></a> */
<a name="line387"></a>function startsWith(value) {
<a name="line388"></a>  return new goog.labs.testing.StartsWithMatcher(value);
<a name="line389"></a>}
<a name="line390"></a>
<a name="line391"></a>
<a name="line392"></a>/**
<a name="line393"></a> * Matches a string that contains the given strings in order.
<a name="line394"></a> *
<a name="line395"></a> * @param {Array.&lt;string&gt;} values The expected value.
<a name="line396"></a> *
<a name="line397"></a> * @return {!goog.labs.testing.StringContainsInOrderMatcher} A
<a name="line398"></a> *     StringContainsInOrderMatcher.
<a name="line399"></a> */
<a name="line400"></a>function stringContainsInOrder(values) {
<a name="line401"></a>  return new goog.labs.testing.StringContainsInOrderMatcher(values);
<a name="line402"></a>}
</pre>


</body>
</html>
